package com.lwq.lwqmicoservice.gateway.controller;

import com.auth0.jwt.interfaces.Claim;
import com.lwq.lwqmicoservice.common.model.Result;
import com.lwq.lwqmicoservice.common.model.SysUser;
import com.lwq.lwqmicoservice.common.util.TokenUtil;
import com.lwq.lwqmicoservice.framework.annotation.PermissionController;
import com.lwq.lwqmicoservice.framework.annotation.PermissionInterface;
import com.lwq.lwqmicoservice.framework.mapper.MenuMapper;
import com.lwq.lwqmicoservice.framework.service.MenuService;
import com.lwq.lwqmicoservice.framework.service.PermissionService;
import com.lwq.lwqmicoservice.gateway.service.AuthenticationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * Description:
 * Author: LiuWenQing
 * Datetime: 2021/11/16
 */
@RestController
@PermissionController(type="authentication")
public class AuthenticationController {

    @Autowired
    private AuthenticationService service;
    @Autowired
    private PermissionService permissionService;
    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private MenuService menuService;
    @Value("${permission.administratorAccounts}")
    private String adminAccount;

    /**
     * 登录
     * @param user
     * @return
     */
    @PostMapping("/login")
    public Object login(@RequestBody @Valid SysUser user){
        return service.doLogin(user);
    }

    /**
     * 获取验证码
     *
     * @return
     */
    @GetMapping("getCaptcha")
    public Object getCaptcha() {
        return null;
    }

    /**
     * 注册接口
     */
    @PostMapping("/register")
    public Result register(@RequestBody @Valid SysUser user){
        service.register(user);
        return Result.ok("注册成功");
    }

    /**
     *修改密码
     */
    @PutMapping("changePass")
    @PermissionInterface(code = "user.update", name = "修改用户")
    public Result changePass(@RequestBody @Valid SysUser user, HttpServletRequest request) {
        if(service.hasPermission(request.getHeader("Authorization"),"user.update")){
            service.changePass(user,request);
            return Result.ok();
        }else{
            return Result.error("令牌错误");
        }
    }

    /**
     * 登录后获取当前用户菜单,登录用户权限
     *
     * @return
     */
    @GetMapping("getPermissions")
    @PermissionInterface(code = "permission.query", name = "查询权限")
    public Map<String, Object> getMenusAndPermissions(HttpServletRequest request) throws Exception {
        if(service.hasPermission(request.getHeader("Authorization"),"permission.query")) {
            Map<String, Object> reMap = new HashMap<>();
            SysUser user = new SysUser();
            Map<String, Claim> authorization = TokenUtil.verifyToken(request.getHeader("Authorization"));
            user.setId(authorization.get("userId").asLong());
            user.setUserName(authorization.get("userName").asString());
            user.setName(authorization.get("name").asString());
            Boolean flag = Arrays.asList(adminAccount.split(",")).contains(user.getUserName());
            reMap.put("userInfo", user);
            reMap.put("permissions", flag ? permissionService.getAllPermissions(null) : permissionService.getPermissionsByUser(user.getId(), null));
            reMap.put("menus", flag ? menuMapper.selectList(null) : menuService.getMenuByUser(user.getId(), null));
            return reMap;
        }else{
            return new HashMap<>();
        }
    }
}
